/***************************************************************************
  textures.c  -  Part of gltris
  -------------------
begin                : March 2001
copyright            : (C) 2001 by David Delbecq (Alias Tchize)
email                : david.delbecq@usa.net
 ***************************************************************************/
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

/****************************************************************************
 * Loads a set of textures in memory, according to definitions in textures.h,*
 * and prepare a texture in memory 'on demand' according to constants        *
 * defined in the same header file.                                          *
 ****************************************************************************/
#include "os_depend.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdlib.h>
#include <stdio.h>
#include "tgaload.h"
#include "textures.h"


GLuint textures [TEX_MAXTEX+1];
char* texturenames[TEX_MAXTEX+1]=TEXFILELIST;
picdata pictures [TEX_MAXPIC+1];
char* picnames[TEX_MAXPIC+1]=TEXPICLIST;

/*
   Load the textures used by the game
   */
void LoadTextures ()
{
    int i;
    image_t p;
    char buffer[1024];
    printf ("Source pictures directory is %s\n",DIRECTORY_PICS);
    for (i=0;i<=TEX_MAXTEX;i++)
    {
        if (texturenames[i]==NULL) /* Texture isn't in a file */
        {
            textures[i]=0;
            continue;
        }
        glGenTextures (TEX_MAXTEX,&textures[i]);
        snprintf (buffer,1024,"%s%s",DIRECTORY_PICS,texturenames[i]);
        printf ("[Loading %s] \t",buffer);
        glBindTexture (GL_TEXTURE_2D,textures[i]);
        glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
        glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glPixelStorei (GL_UNPACK_ALIGNMENT,1);
        tgaLoad  (buffer, &p, TGA_FREE|TGA_ALPHA  );
        printf ("\n");
    }
}
void RemoveTexture (int texNumber)
{
    glDeleteTextures (TEX_MAXTEX,&textures[texNumber]);
    textures[texNumber]=0;
}

void SetTexture (int texNumber, GLint internalFormat, int width, int height, GLenum format, GLenum type, void *texels)
{
    int result;
    RemoveTexture (texNumber);
    glGenTextures (TEX_MAXTEX,&textures[texNumber]);
    glBindTexture (GL_TEXTURE_2D,textures[texNumber]);
    glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri (GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glPixelStorei (GL_UNPACK_ALIGNMENT,1);
    result=gluBuild2DMipmaps (GL_TEXTURE_2D, internalFormat, width, height, format, type, texels);
    if (result !=0) printf ("Probl鑝e pour la cr閍tion de la texture %d: [%s]\n",texNumber,gluErrorString (result));
}

void KillTextures ()  /*Free some datas before quiting*/
{
    int i;
    for (i=0;i<TEX_MAXTEX;i++) RemoveTexture(i);
}

void* GetPicture (int PicRef, int* width, int* height, GLenum* DataType)
{
    if ((PicRef>TEX_MAXPIC) || (PicRef<0)) return NULL;
    *width=pictures[PicRef].width;
    *height=pictures[PicRef].height;
    *DataType=pictures[PicRef].datatype;
    return pictures[PicRef].data;
}

void PrepareTexture (int TextureRef)
{
    if ((TextureRef >TEX_MAXTEX) || (TextureRef<0)) return;
    glBindTexture (GL_TEXTURE_2D,textures[TextureRef]);
}
